package ui.juego.mapamundi

import dominio.datos.Caracteristica
import dominio.ubicaciones.Lugar
import dominio.ubicaciones.Pais
import org.uqbar.arena.bindings.PropertyAdapter
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.layout.VerticalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.List
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.TextBox
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.commons.model.UserException
import ui.juego.utilidades.IconUtils

class EdicionPaisDialog extends Dialog<Pais> {
	
	new(WindowOwner parent, Pais model) {
		super(parent, model)
		iconImage = IconUtils.path
	}
	
	new(WindowOwner parent, Pais model, String title) {
		super(parent,model)
		this.title = title
		iconImage = IconUtils.path
	}
	
	override protected addActions(Panel actionsPanel) {}
	
	override protected createFormPanel(Panel mainPanel) {}
	
	override createMainTemplate(Panel mainPanel) {
		var Panel contentPanel = new Panel(mainPanel) => [
			layout = new VerticalLayout
		]
		
		crearNombrePanel(contentPanel)
		crearCabeceraPanel(contentPanel, "Caracteristicas")[ |
			new EditarCaracteristicaDialog(this, new EditarCaracteristica(modelObject)).open
		]
		crearListadoPanel(contentPanel, "caracteristicas", "dato", Caracteristica)
		crearCabeceraPanel(contentPanel, "Conexiones")[ |
			new EditarConexionDialog(this, new EditarConexion(modelObject)).open
		]
		crearListadoPanel(contentPanel, "conexiones", "nombre", Pais)
		crearCabeceraPanel(contentPanel, "Lugares")[ |
			new EditarLugarDialog(this, new EditarLugar(modelObject)).open
		]
		crearListadoPanel(contentPanel, "lugares", "nombreTipoLugar", Lugar)
		
		new Button(contentPanel) => [
			caption = "Aceptar"
			bindEnabledToProperty("tieneNombreValido")
			onClick [ |
				if (modelObject.tieneLosLugaresRequeridos) accept
				else throw new UserException("El país debe tener 3 (tres) lugares!")
			]
		]
	}
	
	
	def crearNombrePanel(Panel owner) {
		var Panel nombrePanel = new Panel(owner) => [
			layout = new HorizontalLayout
		]
		
		new Label(nombrePanel).text = "Nombre: "
		new TextBox(nombrePanel) => [
			bindValueToProperty("nombre")
		]
	}
	
	def <T> crearCabeceraPanel(Panel owner, String label, ()=>void action) {
		var Panel headPanel = new Panel(owner) => [
			layout = new ColumnLayout(2)
		]
		
		new Label(headPanel).text = label
		new Button(headPanel) => [
			caption = '''Editar «label»'''
			width = 150
			onClick [ |
				action.apply()
			]
		]
	}

	def <T> crearListadoPanel(Panel owner, String modelProperty, String propertyAdapter, Class<T> type) {
		var Panel listPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new List<T>(listPanel) => [
			bindItemsToProperty(modelProperty).adapter = new PropertyAdapter(type, propertyAdapter)
			heigth = 50
			width = 220
		]
		
	}
		
}